package com.itheima.mall.api.sms;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.api.pms.CouponServiceApi;
import com.itheima.mall.mapper.sms.CouponMapper;
import com.itheima.mall.mapper.sms.ProductCategoryRelationMapper;
import com.itheima.mall.mapper.sms.ProductRelationMapper;
import com.itheima.mall.pojo.sms.Coupon;
import com.itheima.mall.pojo.sms.CouponProductCategoryRelation;
import com.itheima.mall.pojo.sms.SmsCouponProductRelation;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.sms.CouponVo;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.LocalDateTime;
import java.util.List;


@DubboService
public class CouponServiceApiImpl implements CouponServiceApi {
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private ProductCategoryRelationMapper productCategoryRelationMapper;
    @Autowired
    private ProductRelationMapper productRelationMapper;

    //添加优惠券
    @Override
    public void addCoupon(CouponVo couponVo) {
        Coupon coupon = new Coupon();
        BeanUtil.copyProperties(couponVo, coupon);

        if (ObjectUtil.isNotEmpty(coupon.getEndTime()) || ObjectUtil.isNotEmpty(coupon.getStartTime())) {
            boolean before = coupon.getEndTime().isBefore(coupon.getStartTime());
            if (before) {
                throw new RuntimeException("结束有效时间不能小于开始时间");
            }
        } else {
            throw new RuntimeException("日期为空");
        }

        coupon.setCount(couponVo.getPublishCount());
        coupon.setUseCount(0);
        coupon.setReceiveCount(0);
        couponMapper.insert(coupon);


        List<SmsCouponProductRelation> productRelationList = couponVo.getProductRelationList();
        for (SmsCouponProductRelation smsCouponProductRelation : productRelationList) {
            smsCouponProductRelation.setCouponId(coupon.getId());
            productRelationMapper.insert(smsCouponProductRelation);
        }

        List<CouponProductCategoryRelation> productCategoryRelationList = couponVo.getProductCategoryRelationList();
        for (CouponProductCategoryRelation couponProductCategoryRelation : productCategoryRelationList) {
            couponProductCategoryRelation.setCouponId(coupon.getId());
            productCategoryRelationMapper.insert(couponProductCategoryRelation);
        }
    }

    //删除优惠券
    @Override
    public void deleteCoupon(Integer id) {
        Coupon coupon = couponMapper.selectById(id);
        LocalDateTime time=LocalDateTime.now();
        boolean before = coupon.getEndTime().isBefore(time);

        if (before){
            couponMapper.deleteById(id);
        }else {
            throw new RuntimeException("不能删除");
        }
    }

    //根据优惠券名称和类型分页获取优惠券列表
    @Override
    public PageResult<Coupon> getCoupon(String name, Integer pageNum, Integer pageSize, Integer type) {
        Page<Coupon> pageInfo = new Page(pageNum, pageSize);
        LambdaQueryWrapper<Coupon> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(name), Coupon::getName, name);
        wrapper.eq(type != null, Coupon::getType, type);
        Page<Coupon> couponPage = couponMapper.selectPage(pageInfo, wrapper);
        PageResult pageResult = new PageResult();
        pageResult.setTotalPage(Convert.toInt(couponPage.getPages()));
        pageResult.setPageNum(Convert.toInt(couponPage.getCurrent()));
        pageResult.setList(couponPage.getRecords());
        pageResult.setPageSize(Convert.toInt(couponPage.getSize()));
        pageResult.setTotal(Convert.toInt(couponPage.getTotal()));
        return pageResult;
    }


    //获取单个优惠券的详细信息
    @Override
    public Coupon selectCoupon(Long id) {
        Coupon coupon = couponMapper.selectById(id);
        return coupon;
    }

    //修改优惠券
    @Override
    public void upadateCoupon(CouponVo couponVo, Long cid) {
        Coupon coupon = new Coupon();
        BeanUtil.copyProperties(couponVo, coupon);
        coupon.setId(cid);
        couponMapper.updateById(coupon);

        //根据优惠券id查找与其相关的分类
        LambdaQueryWrapper<CouponProductCategoryRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CouponProductCategoryRelation::getCouponId, cid);
        List<CouponProductCategoryRelation> couponProductCategoryRelations = productCategoryRelationMapper.selectList(queryWrapper);
        List<Long> ids = CollUtil.getFieldValues(couponProductCategoryRelations, "id", Long.class);
        if (ids.size() != 0) {
            productCategoryRelationMapper.deleteBatchIds(ids);
        }
        List<CouponProductCategoryRelation> productCategoryRelationList = couponVo.getProductCategoryRelationList();
        for (CouponProductCategoryRelation couponProductCategoryRelation : productCategoryRelationList) {
            couponProductCategoryRelation.setCouponId(cid);
            productCategoryRelationMapper.insert(couponProductCategoryRelation);
        }


        LambdaQueryWrapper<SmsCouponProductRelation> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(SmsCouponProductRelation::getCouponId, cid);
        List<SmsCouponProductRelation> couponProductRelations = productRelationMapper.selectList(queryWrapper1);
        List<Long> ids1 = CollUtil.getFieldValues(couponProductRelations, "id", Long.class);
        if (ids1.size() != 0) {
            productRelationMapper.deleteBatchIds(ids1);
        }
        List<SmsCouponProductRelation> productRelationList = couponVo.getProductRelationList();
        for (SmsCouponProductRelation couponProductCategoryRelation : productRelationList) {
            couponProductCategoryRelation.setCouponId(cid);
            productRelationMapper.insert(couponProductCategoryRelation);
        }
    }

}
